package com.chenchao.collection.stream;

import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @encoding : UTF-8
 * @Time : 2020/12/4 19:09
 * @Author : Chen Chao
 * @File : javaSE.com.chenchao.collection.stream.Test01.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 * 流的生成
 */
public class Test01 {
    public static void main(String[] args) {
      /*1.list流的生成*/
        ArrayList<String> strings0 = new ArrayList<>();
        strings0.add("pear");
        strings0.add("apple");

        Stream<String> stream = strings0.stream();

        stream.forEach(System.out::println);
        System.out.println("======================================");

      /*2.set流的生成*/
        HashSet<String> strings1 = new HashSet<>();
        strings1.add("pear");
        strings1.add("apple");
        Stream<String> stream1 = strings1.stream();

        stream1.forEach(System.out::println);
        System.out.println("======================================");

        /*3.Map流的无法直接生成,可以间接生成*/
        HashMap<String, Integer> hashMap = new HashMap<>(5);
        hashMap.put("pear",1);
        hashMap.put("apple",2);

        Stream<String> stream2 = hashMap.keySet().stream();
        Stream<Integer> stream3 = hashMap.values().stream();
        Stream<Map.Entry<String, Integer>> stream4 = hashMap.entrySet().stream();

        stream2.forEach(System.out::println);
        stream3.forEach(System.out::println);
        stream4.forEach(System.out::println);
        System.out.println("======================================");

        /*4.数组流的生成,使用的是Stream的泛型静态方法*/
        String[] strings = new String[]{"pear","apple"};
        Stream<String> stream5 = Stream.of(strings);
        Stream<String> stream6 = Stream.of("pear", "apple");
        Stream<Integer> stream7 = Stream.of(1,2,3);

        stream5.forEach(System.out::println);
        stream6.forEach(System.out::println);
        stream7.forEach(System.out::println);

    }
    public static void test01(){
        /*如果是基本数据类型就会直接把数组看成一个元素*/
        /*对象数组就没有这个问题*/
        Integer[] ints01 = {1, 2, 3, 4, 7, 9};
        int[] ints02 = {1, 2, 3, 4, 7, 9};
        Stream<Integer> ints1 = Stream.of(ints01);
        Stream<int[]> ints021 = Stream.of(ints02);

        /*可以把生成的数组流再转换成intStream*/
        IntStream intStream = ints021.flatMapToInt(Arrays::stream);
        /*基本类型数组可以直接用Arrays.stream(ints02)转换*/
        IntStream stream = Arrays.stream(ints02);

        System.out.println(ints1);

        System.out.println(intStream);
        System.out.println(stream);
    }
}
